﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCadastroTabelaMensal : Form
    {
        private string operacao = "";

        public FormCadastroTabelaMensal()
        {
            InitializeComponent();
        }

        private void FormCadastroTabelaMensal_Load(object sender, EventArgs e)
        {
            //Popular a lista de horarios cadastrados
            this.dataGridViewTabelaMensal.DataSource = ModelBO.getTabelaMensals();
            this.comboBoxHorario.DataSource = ModelBO.getHorarios(TipoPeriodoVO.TipoPeriodo.Mensal);

            setupButtonsControls();
        }

        private void dataGridViewTabelaMensal_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            this.dateTimePickerInicioVigencia.Value = DateTime.Today;
            this.dateTimePickerFimVigencia.Value = DateTime.Today;
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            this.operacao = "excluir";

            List<TabelaMensalVO> tabelaMensalList = (List<TabelaMensalVO>)this.dataGridViewTabelaMensal.DataSource;
            TabelaMensalVO tabelaMensal = tabelaMensalList[this.dataGridViewTabelaMensal.CurrentRow.Index];
            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão da tabela " + tabelaMensal.tabelaMensal, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delTabelaMensal(tabelaMensal);

                int nRow = this.dataGridViewTabelaMensal.CurrentRow.Index;
                this.dataGridViewTabelaMensal.DataSource = ModelBO.getTabelaMensals();
                if (this.dataGridViewTabelaMensal.Rows.Count > 0)
                {
                    dataGridViewTabelaMensal.CurrentCell = dataGridViewTabelaMensal.Rows[Math.Min(nRow, this.dataGridViewTabelaMensal.Rows.Count - 1)].Cells[0];
                    dataGridViewTabelaMensal.Rows[Math.Min(nRow, this.dataGridViewTabelaMensal.Rows.Count - 1)].Selected = true;
                }

                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.textBoxTabelaMensal.Text.Trim().Length == 0)
                erros = erros + "Controle deve ser informado.\r\n";
            if (this.comboBoxHorario.SelectedItem==null)
                erros = erros + "Controle de horário deve ser informado.\r\n";
            if (this.dateTimePickerInicioVigencia.Value > this.dateTimePickerFimVigencia.Value)
                erros = erros + "Vigência inicial MAIOR que vigência final. (Segunda)\r\n";
            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            TabelaMensalVO tabelaMensal = null;

            if (this.operacao.Equals("novo"))
            {

                tabelaMensal = TabelaMensalVO.CreateTabelaMensalVO();

                tabelaMensal.idHorario = int.Parse(this.comboBoxHorario.SelectedValue.ToString());
                tabelaMensal.tabelaMensal = this.textBoxTabelaMensal.Text;
                tabelaMensal.inicioVigencia = this.dateTimePickerInicioVigencia.Value;
                tabelaMensal.fimVigencia = this.dateTimePickerFimVigencia.Value;
                tabelaMensal.valor = this.numericUpDownValor.Value;

                tabelaMensal.idTabelaMensal = (int)ModelBO.newTabelaMensal(tabelaMensal);
            }
            if (this.operacao.Equals("editar"))
            {
                List<TabelaMensalVO> tabelaMensalList = (List<TabelaMensalVO>)this.dataGridViewTabelaMensal.DataSource;
                tabelaMensal = tabelaMensalList[this.dataGridViewTabelaMensal.CurrentRow.Index];

                tabelaMensal.idTabelaMensal = int.Parse(this.textBoxID.Text);
                tabelaMensal.idHorario = int.Parse(this.comboBoxHorario.SelectedValue.ToString());
                tabelaMensal.tabelaMensal = this.textBoxTabelaMensal.Text;
                tabelaMensal.inicioVigencia = this.dateTimePickerInicioVigencia.Value;
                tabelaMensal.fimVigencia = this.dateTimePickerFimVigencia.Value;
                tabelaMensal.valor = this.numericUpDownValor.Value;

                ModelBO.setTabelaMensal(tabelaMensal);
            }
            this.operacao = "salvar";

            List<TabelaMensalVO> listTabelaMensalVOs = ModelBO.getTabelaMensals();
            dataGridViewTabelaMensal.DataSource = listTabelaMensalVOs;
            int nRow = listTabelaMensalVOs.FindIndex(a => a.idTabelaMensal == tabelaMensal.idTabelaMensal);
            dataGridViewTabelaMensal.CurrentCell = dataGridViewTabelaMensal.Rows[nRow].Cells[0];
            dataGridViewTabelaMensal.Rows[nRow].Selected = true;

            setupButtonsControls();
        }


        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewTabelaMensal.Rows.Count > 0 && this.dataGridViewTabelaMensal.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;

            this.comboBoxHorario.Enabled = novo || edit;
            this.textBoxTabelaMensal.Enabled = novo || edit;
            this.dateTimePickerInicioVigencia.Enabled = novo || edit;
            this.dateTimePickerFimVigencia.Enabled = novo || edit;
            this.numericUpDownValor.Enabled = novo || edit;

            this.dataGridViewTabelaMensal.Enabled = !novo && !edit;

            if (!haRows || novo)
            {
                this.textBoxTabelaMensal.Text = "";
            }
            else {
                List<TabelaMensalVO> tabelaMensalList = (List<TabelaMensalVO>)this.dataGridViewTabelaMensal.DataSource;
                TabelaMensalVO tabelaMensal = tabelaMensalList[this.dataGridViewTabelaMensal.CurrentRow.Index];
                this.textBoxID.Text = tabelaMensal.idTabelaMensal.ToString();
                this.comboBoxHorario.SelectedValue = tabelaMensal.idHorario;
                this.textBoxTabelaMensal.Text = tabelaMensal.tabelaMensal;
                this.dateTimePickerInicioVigencia.Value = tabelaMensal.inicioVigencia;
                this.dateTimePickerFimVigencia.Value = tabelaMensal.fimVigencia;
                this.numericUpDownValor.Value = tabelaMensal.valor;

            }
            if (novo || edit){
                this.textBoxTabelaMensal.Focus();
            }
        }

        private void buttonListagem_Click(object sender, EventArgs e)
        {
            FormReportTabelaMensal formx = new FormReportTabelaMensal();
            formx.MdiParent = this.MdiParent;
            formx.Show();
        }

     }
}
